import groupListCell from "./components/groupListCell";
import groupListItem from "./components/groupListItem";

import { scrollMixin } from "../../../utils/mixin"; //引入加载更多

export default {
  mixins: [scrollMixin],
  data() {
    return {
      ROUTERURL: "", //请求地址
      diyTitle: "拼团列表",
      isLoading: false,
      info: {},
      //more
      page: 1, //分页数，当前页数
      isLoadMore: true, //判断是否要加载更多的标志
      total_page: 0, //总页数

      show: false,
      selectID: 0,
      showNowGroup: false//活动拼团中是否显示:true显示，false不显示
    };
  },
  created() {
    this.init();
    this.getData();
  },
  methods: {
    init() {
      this.info = {};
      this.isLoadMore = true;
      this.total_page = 0;
      this.page = 1;

      switch (this.$route.name) {
        case "ywmGroupList":
          this.ROUTERURL = "plugin.ywm-fight-groups.frontend.controllers.fight-groups.index"; //御为名新拼团
          break;
        default:
          this.ROUTERURL = "plugin.ywm-fight-groups.frontend.controllers.fight-groups.index";
          break;
      }
    },

    getData() {
      $http
        .get(this.ROUTERURL, {member_open_group_id: this.$route.params.id || 0}, "加载中")
        .then(
          response => {
            if (response.result === 1) {
              this.isLoadMore = true;
              this.total_page = response.data.last_page;
              if (!this.total_page) {
                this.total_page = 0;
              }
              this.showNowGroup = response.data.set.is_show_be_being_group|| false;
              this.info = response.data.data;
              const {share_title,share_url,share_thumb,share_desc} = response.data.set;
              this.fun.wxShare(
                "",
                {},
                {
                  title: share_title,
                  imgUrl: share_thumb,
                  description: share_desc
                }
              );
              this.isLoading = false;
            } else {
              this.isLoading = false;
              console.log(response.msg);
            }
          },
          response => {}
        )
        .then(v => {});
    },
    goGroup(e) {
      // 跳转拼团详情页
      const { id, storeid } = e;
      let _routerNames = "ywmGroup";
      let query = {}
      if(this.$route.params.id) query.cid = this.$route.params.id;
      if (storeid) {
        this.$router.push(this.fun.getUrl(_routerNames, { id: id, store_id: storeid }));
      } else {
        this.$router.push(this.fun.getUrl(_routerNames, { id: id },query));
      }
    },
    getAllTeam(e){
      // 获取当前拼团参团列表，显示弹窗
      const { id, storeid } = e;
      this.selectID = id;
      this.show = true;
    },
    //获取更多数据，加载更多
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      const that = this;
      if (that.page >= that.total_page) {
        return;
      } else {
        that.page += 1;
        $http
          .get(this.ROUTERURL, { page: that.page, member_open_group_id: this.$route.params.id || 0}, "加载中")
          .then(res => {
            that.isLoadMore = true;
            if (res.result == 1) {
              var nextPageData = res.data.data;
              that.info = that.info.concat(nextPageData); //进行数组拼接
            } else {
              that.page = that.page - 1;
              that.isLoadMore = false;
              return;
            }
          })
          .catch(error => {
            console.log(error);
          });
      }
    },
    //下拉刷新
    loadTop() {
      //console.log('触发下拉');
      this.init();
      this.getData();
      // this.$refs.loadmore.onTopLoaded();
    }
  },
  components: { groupListCell, groupListItem }
};
